#ERIC
from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
import requests
import re
import pymysql
import json
import time
import re
from bs4 import BeautifulSoup
from django.http import HttpResponse

proxies = {
            "http": "http://qf:qf3.1415@202.112.118.7:10086/"
        }



def mysqlConnect():
    # conn = pymysql.connect(host="104.223.203.130", port=3306, user="root", passwd="qf3.1415", db="group_boundpu",
    #                        charset="utf8")
    conn = pymysql.connect(host="127.0.0.1", port=3306, user="root",
                           passwd="", db="ziyuanmenhu",
                           charset="utf8")
    cur = conn.cursor()

    return conn, cur

#插入数据
def runInsertIntoSql(sql,data):

    conn, cur = mysqlConnect()
    cur.execute(sql,data)
    conn.commit()
    cur.close()
    conn.close()

#
def sqlSelect(sql,data):
    conn, cur = mysqlConnect()
    cur.execute(sql,data)
    rows = cur.fetchall()
    cur.close()
    conn.close()
    return rows

#多页构造
def buildingParametersMore(dataJ):
    typeCorr = {
        "TI": "TI",
        "SU": "SU",
        "AU": "AU",
        "JN": "SO",
        "AB": "AB",
    }
    logCorr = {
        "and": "AND",
        "or": "OR",
        "not": "NOT",
        "###": "###"
    }

    allD = []
    for i in range(len(dataJ["values"])):

        tempData = [
            ['GuidedSearchFormData.Index', 1],
            ['GuidedSearchFormData[{}].SearchTerm', 'children'],
            ['GuidedSearchFormData[{}].DbTag', 'TI'],
            ['GuidedSearchFormData[{}].Op', ''],
        ]

        try:
            tempData[0][1] = i + 1

            tempData[1][0] = tempData[1][0].format(i + 1)
            tempData[1][1] = dataJ["values"][i]

            tempData[2][0] = tempData[2][0].format(i + 1)
            tempData[2][1] = typeCorr[dataJ["type"][i]]

            tempData[3][0] = tempData[3][0].format(i + 1)

            if len(dataJ["log"]) != 0:
                tempData[3][1] = logCorr[dataJ["log"][i - 1]]

        except:
            raise

        allD.extend(tempData)



    return allD



def resultData():
    data = {
        "websiteType" : 0,
        "titleName":"",
        "titleHref": "",
        "source":"",
        "label":"",
        "volume":"",
        "summary":"",
        "detailId":'',
        "publishTime" :"",
        "category" : "",
        "quote" :"",
        "abstract": "",
        "doi": "",
        "downLoad": "",
        "keyword": [],
        "author": [],

    }
    return data

def updateCookie(cookies,tempCookies):

    for k, v in tempCookies.items():
        cookies[k] = v

    return cookies

def updateCookiesToMysql(uid,webId,cookies):

    # print(json.loads(str((json.dumps(cookies)))))
    # exit()

    sql = "select count(1) as num from cookies where uid=%s and webId=%s"
    data = sqlSelect(sql, [uid, webId])
    if data[0][0] == 0:
        sql = '''insert into cookies(uid,cookies,webId,createTime) values(%s,%s,%s,%s)'''
        runInsertIntoSql(sql, [uid, str(json.dumps(cookies)), webId, int(time.time())])
    else:
        sql = '''update cookies set cookies=%s where uid=%s and webId=%s'''
        runInsertIntoSql(sql, [str(json.dumps(cookies)), uid, webId])

def getCookiesFromMysql(uid,webId):
    sql = "select cookies from cookies where uid=%s and webId=%s"
    data = sqlSelect(sql, [uid, webId])

    return json.loads(data[0][0])

def first():
    headers = {
        'Proxy-Connection': 'keep-alive',
        'Cache-Control': 'max-age=0',
        'Proxy-Authorization': 'Basic cWY6cWYzLjE0MTU=',
        'Origin': 'http://search.ebscohost.com',
        'Upgrade-Insecure-Requests': '1',
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Referer': 'http://search.ebscohost.com/Community.aspx?community=y&authtype=ip&stsug=AjM32pD8yaKJBdTS2Qi9cKgDlm-yATOSyGoYlx3SCOYkm9_YBywjoDcQqoh96EWOyfFPNnEzsBfueejAVXiplX5eZV1aIXUvKNAJBKIh0QtvcN5fTRctPrFT9X-cUinGeK1hdFkEezE7W5bQnKQohvqRyq9CG-oWTznHeS7BZ0p3UbQ&IsAdminMobile=N&encid=22D731263C5635173726355632253C47360372C372C375C374C373C371C376C33013',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    }

    params = (
        ('community', 'y'),
        ('authtype', 'ip'),
        ('encid', '22D731263C5635173726355632253C47360372C372C375C374C373C371C376C33013'),
        ('IsAdminMobile', 'N'),
        ('authpid', 'ehost'),
    )

    data = {
      '__EVENTTARGET': '_ctl18',
      '__EVENTARGUMENT': '',
      '__VIEWSTATE': 'j0Cm0PT361ExT1dSebhS45ZsUlR+XNJpkwB36/0E05XhyyxC6VdSUR/Yx3ZM67TFvP7WiBjlB0OYNCfPYJv1rIIYvpQtaDbrEk8s5IChQz9dib09u/CbSXGRGBSAYdNTNHpjGw==',
      '__PREVIOUSPAGE': '832syxT-Ep5f4bn61m4dqKaB8KV6q17Xxmls84BLgBdcOMGbkJPXdxhR-oW5noPkq3oD4Ir7I0MnVIVoFRu70AfeE3g1',
      '__EVENTVALIDATION': 'A7ykKwJsb0zSgy4jCiAsG/5+M9aYShtyvtNRZuG/PygD+BFO15zL0jygpj9wglp6icNm3pBkZPWibHd/IZ5CStInoaCvGvc2Nc9qcRrrZl98L//IqkAVX+8UbOYRj9+bl2M12hdYG8YQPnArxvIineS8S8lyaAskX2lYMFe4UJ/NSaUC38nGccXkzbWi0VpjW3WpNrVWHz9+1ZB+gY8pCk2Ayfxdk8UT/msBzeG/gfQIwo5G5sGQUsFWgSGIM4L8FmUD2wV3pk/R0Xn2F1YgYiYFV56k1/OUtbnLF+ZrMkRA4IpadyP8h1Jn8qFoKwYs7Ld93g==',
      'cpToken': '723731163C1635973766352632453E9226E362D36213659364E323E331133603',
      'selectServicesToken': ''
    }


    s = requests.session()
    response = s.post('http://search.ebscohost.com/Community.aspx', headers=headers, params=params , data=data,proxies = proxies)
    cookies = s.cookies.get_dict()
    return cookies



# exit()
def second(cookies,sid):
    headers = {
        'Proxy-Connection': 'keep-alive',
        'Cache-Control': 'max-age=0',
        'Proxy-Authorization': 'Basic cWY6cWYzLjE0MTU=',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        # 'Referer': 'http://web.a.ebscohost.com/ehost/search/selectdb?vid=0&sid=599a9512-9642-4963-9bf1-f9805692cc44%40sessionmgr4008',
        'Origin': 'http://web.a.ebscohost.com',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    }

    params = (
        ('vid', '0'),
        ('sid', sid),
    )

    s = requests.session()
    response = s.get('http://web.a.ebscohost.com/ehost/search/selectdb', headers=headers, params=params, cookies=cookies,proxies = proxies)
    return s.cookies.get_dict()

def formData(response):
    allData = []
    # 获取sid并且更新返回
    soup = BeautifulSoup(response.text, "html5lib")
    # print(soup)
    # exit()

    lis = soup.select(".result-list-li")

    page_number1 = soup.select(".content-header h1")[0].text
    px = re.compile("共 (.*?) 个")
    allNum = px.findall(page_number1)[0].strip().replace(",", '')

    for li in lis:
        tempData = resultData()
        try:
            tempData["titleName"] = li.select("h3 > a")[0].text
        except:
            pass
        try:
            tempData["titleHref"] = li.select("h3 > a")[0]["href"]
        except:
            pass
        try:
            tempData["abstract"] = li.select(".abstract")[0].text
        except:
            pass
        allData.append(tempData)
    return allData,allNum

#得到搜索页面
def getSearchWeb(cookies,sid):

    headers = {
        'Proxy-Connection': 'keep-alive',
        'Cache-Control': 'max-age=0',
        'Proxy-Authorization': 'Basic cWY6cWYzLjE0MTU=',
        'Origin': 'http://web.b.ebscohost.com',
        'Upgrade-Insecure-Requests': '1',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Referer': 'http://web.b.ebscohost.com/ehost/search/advanced?vid=1&sid=f6a9e11b-44d8-46f6-956c-8a8441f088cd%40sessionmgr102',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    }

    params = (
        ('vid', '0'),
        ('sid', sid),
    )

    data = {
        'RelRequestPath': 'search/selectdb',
        '__EVENTTARGET':'ctl00$ctl00$MainContentArea$MainContentArea$SelectDbControl$dbList$ctl05$ctl00$titleLink',
        '__EVENTARGUMENT': '',
        '__sid': sid,
        '__vid': '0',
        '__CUSTOMVIEWSTATE': cookies["custo"],
        '__VIEWSTATE': '',
        'ajax': 'enabled'
    }
    cookies.pop('custo')

    s = requests.session()
    response = s.post('http://web.a.ebscohost.com/ehost/search/selectdb', headers=headers, params=params,cookies=cookies, data=data,proxies = proxies)

    cookies = s.cookies.get_dict()

    return cookies

def getDataOnePage(keyword,cookies,sid):
    # import requests


    headers = {
        'Proxy-Connection': 'keep-alive',
        'Cache-Control': 'max-age=0',
        'Proxy-Authorization': 'Basic cWY6cWYzLjE0MTU=',
        'Origin': 'http://web.a.ebscohost.com',
        'Upgrade-Insecure-Requests': '1',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Referer': 'http://web.a.ebscohost.com/ehost/resultsadvanced?vid=3&sid=59801a8e-c1b6-4602-b01a-1c1a3b429ec6%40sdc-v-sessmgr05&bquery=TI+children&bdata=JmRiPWE5aCZkYj1idGgmbGFuZz16aC1jbiZ0eXBlPTEmc2l0ZT1laG9zdC1saXZl',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    }

    params = (
        ('sid', sid),
        ('vid', '1'),
    )

    data = [
        ('RelRequestPath', 'search/advanced'),
        ('__EVENTTARGET', ''),
        ('__EVENTARGUMENT', ''),
        ('__sid', sid),
        ('__vid', '0'),
        ('__CUSTOMVIEWSTATE',
         'H4sIAAAAAAAEAJWRMU8bMRTHFSdOAknJgIiAITmEynYXLoqiBhBLpw7tULWsJ3N2chY++zj7IuiGWGBjQAxIDEx0gI/Q8mWqRP0UrX29tCe6gGTZ1t/v/X9+7/0qNHAD1my32zWr1+s3mqC1uIcYxUiRj+QwIVK9F5iAQhMMGzjbQMHElRjiIwi/BLbPtVY0GvQZkhI2ubDlAeUWwmPEfYItEgipdBTU+dWZicmo7FFJ9xkJMAZVo7SL5vrqX8gfYSETZns9BZo3UM7Z1T6gMR3pz3+OGVwJlIq2Oh2ZRJGIlUP2pS8cX4QdbVhJw721d4qEb0XCFSg2yzlAWmWpVQpiMoSbxkk+tTI1pXZpdZ0opmPkHzuBCllrTncQ2ShRAq4OhVAk9rJ3LxKM6oNRfmAaulSo/7y+mN6dTC4fpw9XGIAnf2iXofMcvmaEMqW3K3A5g6aiJ4ZeIkke+ePb4/Ty6+TmdnL/Hete/od88zLkui+i45iOAqXhSxn8r5Yjl6fnZ5ObUwzmcmMrfSJHCtY31t3+YNvqbroDS49ofjbhmrlU4eudNdu2dCHxliVdd9Dv9gZOiCh3iC84o8qybHsX/wauqGKE1wIAAA=='),
        ('__ScreenResolution', '1440 : 900'),
        ('__VIEWSTATE', ''),
        ('GuidedSearchFormData.Index', '1'),
        ('GuidedSearchFormData.Index', '2'),
        ('GuidedSearchFormData.Index', '3'),
        ('GuidedSearchFormData[1].SearchTerm', keyword),
        ('GuidedSearchFormData[1].DbTag', ''),
        ('GuidedSearchFormData[2].Op', 'AND'),
        ('GuidedSearchFormData[2].SearchTerm', ''),
        ('GuidedSearchFormData[2].DbTag', ''),
        ('GuidedSearchFormData[3].Op', 'AND'),
        ('GuidedSearchFormData[3].SearchTerm', ''),
        ('GuidedSearchFormData[3].DbTag', ''),
        ('PerformSearchSettingValue', '3'),
        ('searchMode', 'Bool'),
        ('common_TI', ''),
        ('common_DT1', ''),
        ('common_DT1_FromYear', ''),
        ('common_DT1_ToMonth', ''),
        ('common_DT1_ToYear', ''),
        ('common_PT50', ''),
        ('common_GL', ''),
        ('common_CY3', ''),
        ('ajax', 'enabled'),
    ]

    s = requests.session()
    response = s.post('http://web.a.ebscohost.com/ehost/Search/PerformSearch', headers=headers, params=params,
                             cookies=cookies, data=data,proxies = proxies)
    # print(response.text)
    result, allNum = formData(response)
    cookies = s.cookies.get_dict()
    return cookies, result, allNum



def getZ():
    # 选取类型
    cookies = first()

    EHost2 = cookies["EHost2"]
    px = re.compile("sid=(.*?)&")
    sid = px.findall(EHost2)[0]

    cookies = getCusto(cookies, sid)

    # 进入到高级搜索界面
    tempCookies = getSearchWeb(cookies, sid)
    cookies = updateCookie(cookies, tempCookies)

    return cookies

def decode_base64(data):
    import base64

    """Decode base64, padding being optional.
    :param data: Base64 data as an ASCII byte string
    :returns: The decoded byte string.

    """
    missing_padding = 4 - len(data) % 4
    if missing_padding:
        data += '=' * missing_padding
    return base64.b64decode(data)

def getUserByToken(token,type=0,catetory="pc"):
    # return 'fff'
    #0:pc 1 后台 2：微信
    try:

        # return token
        if type == 0:

            token = (json.loads(str(decode_base64(token), encoding="utf8")))

            # return
            access_token = token["token"]["access_token"]

            url = "http://login.qfclou.com/index/oauth_server/resource?access_token={}".format(access_token)

            js = requests.get(url).json()
            addrId = js["data"]["organizationid"]
            uid = js["data"]["id"]
            uname = js["data"]["name"]

            # return js
        elif type == 1:
            url = "http://login.qfclou.com/agent/common/check_token"
            js = requests.post(url, {"token": token}).json()
            # addrId = js["other"]["userinfo"]["organizationid"]
            addrId = js["other"]["userinfo"]["organizationid"]
            uid = js["other"]["userinfo"]["id"]
            uname = js["other"]["userinfo"]["name"]
        elif type == 2:
            url = "http://weixin.qfclou.com/index/weixin/check_token"

            js = requests.post(url, data={"token": token,"type":catetory,"str":"qf_weixin","key":"y&ca1UtDMQRrZW38"}).json()
            # return js
            addrId = js["userinfo"]["organizationid"]
            uid = js["userinfo"]["id"]
            uname = js["userinfo"]["name"]


        return [addrId,uid,uname]

    except:
        return -1

def getCusto(cookies,sid):

    headers = {
        'Proxy-Connection': 'keep-alive',
        'Proxy-Authorization': 'Basic cWY6cWYzLjE0MTU=',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Referer': 'http://web.a.ebscohost.com/start?key=10.83.8.64_8000_249620857&site=ehost&community=y&IsAdminMobile=N&return=y',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    }

    params = (
        ('vid', '0'),
        ('sid', sid),
    )

    response = requests.get('http://web.a.ebscohost.com/ehost/search/selectdb', headers=headers, params=params,
                            cookies=cookies,proxies = proxies)

    soup = BeautifulSoup(response.text, "html5lib")

    cookies["custo"] = soup.select("input#__CUSTOMVIEWSTATE")[0]["value"]

    return cookies

def index(request):

    page = 1
    data = {"values": ["cell"], "type": ["TI"], "log": []}
    startYear = 1995
    endYear = 2020


    if page == 1:
        dd = buildingParametersMore(data)

        # print(dd)
        cookies = getZ()

        EHost2 = cookies["EHost2"]
        px = re.compile("sid=(.*?)&")
        sid = px.findall(EHost2)[0]

        #获取第一页数据
        tempCookies, allData,allNum = getDataOnePage(data["values"][0],cookies,sid)

        return HttpResponse(json.dumps({
            "collection": allData,
            "allNum": 0
        }), content_type="application/json")
